<%@ page language="java" import="java.util.*,com.demodis.domain.TblUser" pageEncoding="UTF-8"%>
<%
	String deterUrl = ((TblUser)request.getSession().getAttribute("user")).getDeterUrl();
%>
<script type="text/javascript" charset="UTF-8">
	var moduls_persDuty_datagrid;
	$(function() {
		moduls_persDuty_datagrid = $('#moduls_persDuty_datagrid').datagrid({
			url : '${pageContext.request.contextPath}/persDutyAction!query.action',
			fit : true,
			fitColumns : false,
			pagination : true,
			checkOnSelect : false,
			selectOnCheck : false,
			singleSelect : true,
			idField : 'pdId',
			//pagination : true,
			border : false,
			toolbar : '#moduls_persDuty_datagrid_toolbar',
			columns : [ [ {
				title : 'ID',
				field : 'dpId',
				width : 50,
				checkbox : true
			}, {
				title : '所属部门',
				field : 'groupName',
				width : 100
			}, {
				title : '姓名',
				field : 'userRealName',
				width : 100
			}, {
				field : 'action',
				title : '动作',
				width : 100,
				formatter : function(value, row, index) {
					if (row.id == '0') {
						return '系统角色';
					} else {
						return formatString('<img onclick="moduls_persDuty_editFun(\'{0}\');" src="{1}"/>&nbsp;&nbsp;&nbsp;<img onclick="moduls_persDuty_deleteFun(\'{2}\');" src="{3}"/>', row.pdId, 'jslib/jquery-easyui-1.3.1/themes/icons/pencil.png', row.pdId, 'jslib/jquery-easyui-1.3.1/themes/icons/cancel.png');
					}
				}
			} ] ]
		});
		
		initGroupListMenu();
	});

	function initGroupListMenu() {
		$.ajax({
			type : 'POST',
			url : 'groupAction!getGroupComboBoxData.action',
			cache : false,
			dataType : 'JSON',
			success : function(data) {
				for(var i = 0; i < data.length; i++) {
					$('div[id=groupListMenu]').menu('appendItem', {
			    		id: data[i].groupId,
			    		text: data[i].groupName,
			    		iconCls: 'icon-group'
			    	});
				}
			}
		});
		
		$("div[id=groupListMenu]").menu({   
		    onClick:function(item){   
		    	$('#moduls_persDuty_datagrid').datagrid('load', {
		        	groupId : item.id == undefined ? 0 : item.id
		        });   
		    }   
		}); 
	}

	function moduls_persDuty_addFun() {
		moduls_persDuty_datagrid.datagrid('uncheckAll').datagrid('unselectAll').datagrid('clearSelections');
		$('<div/>').dialog({
			href : '${pageContext.request.contextPath}/moduls/fun/addPersDuty.jsp',
			width : 700,
			height : 400,
			modal : true,
			title : '添加用户职责',
			buttons : [ {
				text : '保存',
				iconCls : 'icon-save',
				handler : function() {
					var d = $(this).closest('.window-body');
					$('#moduls_fun_addPersDuty_addForm').form('submit', {
						url : '${pageContext.request.contextPath}/persDutyAction!add.action',
						success : function(result) {
							var r = $.parseJSON(result);
							if (r.success) {
								moduls_persDuty_datagrid.datagrid('load');
								d.dialog('destroy');
							}
							$.messager.show({
								title : '提示',
								msg : r.msg
							});
						}
					});
				}
			}, {
				text : '取消',
				iconCls : 'icon-remove',
				handler : function() {
					$(this).closest('.window-body').dialog('destroy');
				}
			} ],
			onClose : function() {
				$(this).dialog('destroy');
			}
		});
	}

	function moduls_persDuty_editFun(id) {
		$('#moduls_persDuty_datagrid').datagrid('uncheckAll').datagrid('unselectAll').datagrid('clearSelections');
		var index = $('#moduls_persDuty_datagrid').datagrid('getRowIndex', id);
		$('#moduls_persDuty_datagrid').datagrid('checkRow', index);
		
		$('<div/>').dialog({
			href : '${pageContext.request.contextPath}/moduls/fun/editPersDuty.jsp',
			width : 700,
			height : 400,
			modal : true,
			title : '编辑用户职责',
			buttons : [ {
				text : '保存',
				iconCls : 'icon-save',
				handler : function() {
					var d = $(this).closest('.window-body');
					$('#moduls_fun_editPersDuty_editForm').form('submit', {
						url : '${pageContext.request.contextPath}/persDutyAction!modify.action',
						success : function(result) {
							try {
								var r = $.parseJSON(result);
								if (r.success) {
									moduls_persDuty_datagrid.datagrid('load');
									d.dialog('destroy');
								}
								$.messager.show({
									title : '提示',
									msg : r.msg
								});
							} catch (e) {
								$.messager.alert('提示', result);
							}
						}
					});
				}
			}, {
				text : '取消',
				iconCls : 'icon-remove',
				handler : function() {
					$(this).closest('.window-body').dialog('destroy');
				}
			} ],
			onClose : function() {
				$(this).dialog('destroy');
			}
		});
	}

	function moduls_persDuty_deleteFun(id) {
		$('#moduls_persDuty_datagrid').datagrid('uncheckAll').datagrid('unselectAll').datagrid('clearSelections');
		var index = $('#moduls_persDuty_datagrid').datagrid('getRowIndex', id);
		$('#moduls_persDuty_datagrid').datagrid('checkRow', index);
		moduls_persDuty_removeFun();
		
	}
	
	function moduls_persDuty_removeFun() {
		var rows = $('#moduls_persDuty_datagrid').datagrid('getChecked');
		var ids = [];
		if (rows.length > 0) {
			$.messager.confirm('确认', '您是否要删除当前选中的项目？', function(r) {
				if (r) {
					for ( var i = 0; i < rows.length; i++) {
						ids.push(rows[i].pdId);
					}
					$.ajax({
						url : '${pageContext.request.contextPath}/persDutyAction!delete.action',
						data : {
							ids : ids.join(',')
						},
						dataType : 'json',
						success : function(result) {
							if (result.success) {
								$('#moduls_persDuty_datagrid').datagrid('load');
								$('#moduls_persDuty_datagrid').datagrid('uncheckAll').datagrid('unselectAll').datagrid('clearSelections');
							}
							$.messager.show({
								title : '提示',
								msg : result.msg
							});
						}
					});
				}
			});
		} else {
			$.messager.show({
				title : '提示',
				msg : '请勾选要删除的记录！'
			});
		}
	}

</script>
<table id="moduls_persDuty_datagrid"></table>

<div id="moduls_persDuty_datagrid_toolbar">
	<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true" onclick="javascript:moduls_persDuty_addFun()">增加</a>
	<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-remove',plain:true" onclick="javascript:moduls_deterPerson_addFun()">批量删除</a>
	<a href="javascript:void(0)" class="easyui-menubutton" data-options="menu:'#groupListMenu',iconCls:'icon-detailed'">按部门查看</a>
	
	<div id="groupListMenu" style="width:190px; height: 200px; overflow: auto;">  
	</div>
</div>
